//
//  CarTableViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/8/3.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "CarTableViewController.h"
#import "CarSearch.h"
#import "IndexCarModel.h"
#import "CarlistCellMode.h"
#import "CarDetailmodel.h"
#import "CarTableCellTableViewCell.h"
#import "SaixuanViewController.h"
#import "CarDetailViewController.h"
#import "FindCarTableViewController.h"
#import "LoadingView.h"
#import <MJRefresh.h>
#import "CaChidTableViewController.h"
#import "CarbrandModel.h"
#import "CarDetail.h"
#import "CarprogChildmode.h"
#import "CatType.h"
#import "SearchModaViewController.h"
#import "AMBlurView.h"
#import "SearchBarView.h"
#import "SearchTableViewCell.h"
#import "SearchMode.h"
#import "CZSearchBar.h"
#import "SearchTableViewController.h"
#import "History.h"
#import "ClearBtn.h"
@interface CarTableViewController ()<UITableViewDelegate,UITableViewDataSource,FindCarDataloadingDelegate,SXViewControllerDelegate,UISearchBarDelegate,UISearchResultsUpdating,UISearchControllerDelegate,UISearchDisplayDelegate,UITextFieldDelegate,SearchTableViewDelegate>
{
    BOOL _isAll;
    BOOL _isNeedR;
    BOOL _isClear;
    BOOL _isShowSearch;
}
//@property (nonatomic,strong) FindCarTableViewController *findvc;

@property (nonatomic,strong)  CZSearchBar *search;
@property (nonatomic,strong) LoadingView *loading;
@property (nonatomic,strong) NSMutableArray *indexcararray;
@property (nonatomic,copy) NSString *conn;

@property (nonatomic,assign) BOOL isfromsearch;
@property (nonatomic,copy) NSString *searchcond;
@property (nonatomic,copy) NSString *searchconddetail;
@property (nonatomic,assign) BOOL isShowNavigationBar;

@property (nonatomic,strong) NSMutableArray *searcharray;
@property (nonatomic,strong) ClearBtn *clearbtn;
@end

@implementation CarTableViewController
-(NSMutableArray *)indexcararray{
    if (_indexcararray==nil) {
        _indexcararray=[NSMutableArray array];
    }
    return _indexcararray;
}

-(NSMutableArray *)searcharray{
    if ( _searcharray==nil) {
        _searcharray=[NSMutableArray array];
    }
    return  _searcharray;
}

-(void)SaixuanClearClick:(SaixuanViewController *)viewcontroller{
    self.conn=nil;
    self.tiaojian=nil;
    [self needupdate];
}

-(void)searchCilck:(PUSHTYPE)type WithString:(NSString *)string{
    _isShowSearch=NO;
    self.isfromsearch= [XHUserDeflaut boolForKey:ISFROMSEARCH];
    self.searchcond=[XHUserDeflaut stringForKey:SEARCHCOND];
    self.search.text=self.searchcond;
    
    [self setNeedsStatusBarAppearanceUpdate];
    if (type==PUSHTYPECar) {
    
        [self needupdate];

    }
}


-(void)searchDidCancelClick{
    _isShowSearch=NO;
    [self setNeedsStatusBarAppearanceUpdate];
}

//-(void)SSchecked:(NSNotification *)notice{
//    self.tiaojian=notice.userInfo;
//    self.conn=[self getswicthparmas:self.tiaojian];
//    _isAll=NO;
//    [self needupdate];
//}
//-(void)needSwitch{
//    [self switchDetail];
//    [self needupdate];
//}

//-(void)switchDetail{
//    self.findvc.view.hidden=YES;
//    self.tableView.hidden=NO;
//}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.search removeObserver:self forKeyPath:@"text"];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return _isShowSearch?UIStatusBarStyleDefault: UIStatusBarStyleLightContent;
}
//-(LoadingView *)loading{
//    if (_loading==nil) {
//        _loading=[LoadingView loading];
//        _loading.frame=self.view.frame;
//        _loading.hidden=YES;
//    }
//    [self.view bringSubviewToFront:_loading];
//    return _loading;
//}
//-(FindCarTableViewController *)findvc{
//    if (_findvc==nil) {
//        _findvc=[FindCarTableViewController findcar];
//        _findvc.view.frame=CGRectMake(0, 64, XHScreenwidth, XHScreenheight-50-64);
//        [self addChildViewController:_findvc];
//        [self.view addSubview:_findvc.view];
//        _findvc.view.hidden=YES;
//        _findvc.delegate=self;
//    }
//    return _findvc;
//}
//-(void)loadingCompleted{
//    self.loading.hidden=YES;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle=NO;
    self.navigationItem.title=@"找车源";
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    self.tableView.backgroundColor=bgGarycolor;
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    [self setupRightItem:NO];
    
    CZSearchBar *search=[[CZSearchBar alloc]initWithFrame:CGRectMake(16, 16, XHScreenwidth-32, 30)];
    search.placeholder=@"可输入品牌、车型、车源类型、配置";
    search.background=[UIImage imageWithStretchableName:@"searchbar_textfield_background"];
    search.font=[UIFont systemFontOfSize:14];
    
    search.enabled=NO;
    UIButton *button=[[UIButton alloc]initWithFrame:search.frame];
    [button addTarget:self action:@selector(togosearch) forControlEvents:UIControlEventTouchUpInside];
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, 48)];
    
    [view addSubview:search];
    [view addSubview:button];
    ClearBtn *clear=[[ClearBtn alloc]initWithFrame:CGRectMake(button.width-40, 0, 40, 30)];
    [clear setImage:[UIImage imageNamed:@"clearbtn"] forState:UIControlStateNormal];
    [clear addTarget:self action:@selector(clearsearchbtnclick) forControlEvents:UIControlEventTouchUpInside];
        [button addSubview:clear];
  
    [search addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    self.clearbtn=clear;
    self.search=search;
    self.search.delegate=self;
    self.tableView.tableHeaderView=view;
    self.tableView.contentInset=UIEdgeInsetsMake(0, 0, 8, 0);
    
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    
//        self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:search contentsController:self];
//    self.searchController.active=NO;
//    self.searchController.searchResultsDelegate=self;
//    self.searchController.searchResultsDataSource=self;
//    self.searchController.searchResultsTableView.backgroundColor=RGBACOLOR(0, 0, 0, 0.4);
//    self.searchController.searchResultsTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
//    self.searchController.delegate=self;
//    [self.tableView registerNib:[UINib nibWithNibName:@"CarTableCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"CarCell"];
    [self.view addSubview:self.loading];
    self.tableView.header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.header.automaticallyChangeAlpha=YES;
    self.tableView.footer=[MJRefreshBackNormalFooter footerWithRefreshingTarget: self refreshingAction:@selector(loadmoredata)];
    if (self.tiaojian!=nil) {
      self.conn=[self getswicthparmas:self.tiaojian];
    }
    
   
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SSchecked:) name:NoticeCartory object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needclearupdate:) name:NoticeUpdate object:nil];
}
-(void)clearsearchbtnclick{
    [self clearsearch];
    [self needupdate];
}
-(void)back{
    [self clearsearch];
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     [self.tableView.header beginRefreshing];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"text"]) {
        if (self.searchcond==nil) {
            self.clearbtn.hidden=YES;
        }else{
            self.clearbtn.hidden=NO;
        }
    }
}



-(void)clearsearch{
    
    self.search.text=nil;
    
    [XHUserDeflaut setBool:NO forKey:ISFROMSEARCH];
    
    [XHUserDeflaut setObject:nil forKey:SEARCHCOND];
//            [self.searchController setActive:NO animated:YES];
//            [self.searcharray removeAllObjects];
//    [self needupdate];

}
-(void)togosearch{
    _isShowSearch=YES;
    [self setNeedsStatusBarAppearanceUpdate];
    SearchTableViewController *searchvc=[SearchTableViewController viewController];
    searchvc.modalPresentationStyle=UIModalPresentationOverFullScreen;
    searchvc.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
    searchvc.delegate=self;
    [self presentViewController:searchvc animated:YES completion:^{
        
    }];

}
-(void)SaixuanOkClick:(NSDictionary *)tiaojian{
    self.tiaojian=tiaojian;
    self.conn=[self getswicthparmas:tiaojian];
    [self needupdate];
}

//-(void)viewDidDisappear:(BOOL)animated{
//    [super viewDidDisappear:animated];
//    [self clearsearch];
//}

//- (BOOL)searchDisplayController:(UISearchDisplayController *)controller{
//    controller.searchResultsTableView.hidden=YES;
//    
//        return YES;
//}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    if (self.search.text.length>0) {
        [XHUserDeflaut setBool:YES forKey:ISFROMSEARCH];
        [XHUserDeflaut setObject:self.searchDisplayController.searchBar.text forKey:SEARCHCOND];
    
//        [self.searchController setActive:NO animated:YES];
        [self needupdate];
        

    }else{
        
        [XHUserDeflaut setBool:NO forKey:ISFROMSEARCH];
       
        [XHUserDeflaut setObject:nil forKey:SEARCHCOND];
//        [self.searchController setActive:NO animated:YES];
//        [self.searcharray removeAllObjects];
        [self needupdate];
    }
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString

{
   
    for(UIView *subview in controller.searchResultsTableView.subviews) {
    
    if([subview isKindOfClass:[UILabel class]]) {
        
        [(UILabel*)subview setText:nil];
        
    }
    
}
    return YES;
}

//-(void)needclearupdate:(NSNotification *)notice{
//    NSNumber *number= notice.userInfo[@"needupdate"];
//    if ([number integerValue]==0) {
//        
//    }else{
//        [self clearConn];
//        [self needupdate];
//    }
//}
-(void)needupdate{
    
    [self.tableView.header beginRefreshing];
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([searchText isEqualToString:@""]) {
        [XHUserDeflaut setBool:NO forKey:ISFROMSEARCH];
        
        [XHUserDeflaut setObject:nil forKey:SEARCHCOND];
        //        [self.searchController setActive:NO animated:YES];
        //        [self.searcharray removeAllObjects];
        [self needupdate];

    }
}

//-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
//    NSLog(@"%@",searchText);
//    if (searchText.length>0) {
//        if (![searchText isEqualToString:@"有限"]&&![searchText isEqualToString:@"公司"]&&![searchText isEqualToString:@"有限公"]&&![searchText isEqualToString:@"有限公司"]) {
//
//        NSLog(@"%@",searchText);
//        NSMutableDictionary *params=[NSMutableDictionary dictionary];
//        
//        params[@"cond"]=searchText;
//        params[@"action"]=@"s_car";
//        params[@"s_action"]=@"count";
//        
//        [PostUitls Post:searchurl parameters:params success:^(id response) {
//            [self.searcharray removeAllObjects];
//            [SearchMode setupReplacedKeyFromPropertyName:^NSDictionary *{
//                return @{
//                         @"ID":@"id"
//                         };
//            }];
//            NSDictionary *dict=response;
//            NSNumber *state=dict[@"state"];
//            if ([state integerValue]==400) {
//                NSArray *array=    [SearchMode objectArrayWithKeyValuesArray:dict[@"data"]];
//                [self.searcharray addObjectsFromArray:array];
//                
//            }
//            if (self.searcharray.count>0) {
//                self.searchController.searchResultsTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
//            }else{
//                self.searchController.searchResultsTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
//            }
//           [self.searchController.searchResultsTableView reloadData];
//            
//        } failure:^(NSError *error) {
//            
//        }];
//        
//    }
//    }else{
//        
//        [self.searcharray removeAllObjects];
//        [self.searchController.searchResultsTableView reloadData];
//    }
//}


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)_controlNavigationBar:(BOOL)show {
    CGRect naivgationBarFrame = self.navigationController.navigationBar.frame;
    CGFloat padding = 0;
    if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) {
        padding = 20;
    }
    if (show) {
        naivgationBarFrame.origin.y += 44 + padding;
    } else {
        naivgationBarFrame.origin.y -= 44 + padding;
    }
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.navigationController.navigationBar.frame = naivgationBarFrame;
    } completion:^(BOOL finished) {
        self.isShowNavigationBar = show;
    }];
}

-(void)refresh{
    self.isfromsearch= [XHUserDeflaut boolForKey:ISFROMSEARCH];
    self.searchcond=[XHUserDeflaut stringForKey:SEARCHCOND];
  
    self.search.text=self.searchcond;
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    NSString *url;
    if (self.isfromsearch) {
        params[@"action"]=@"s_car";
        params[@"s_action"]=@"mydata";
        params[@"cond"]=self.searchcond;
//        if (self.searchconddetail) {
//            params[@"sesion"]=self.searchconddetail;
//        }
        url=searchurl;
    }else{
        url=sscarurl;
        if (self.conn&&![self.conn isEqualToString:@""]) {
            params[@"action"]=@"where";
            params[@"cond"]=self.conn;
            
        }else{
            params[@"action"]=@"where";
        }
    }
    params[@"num"]=Kpagesize;
    
    [PostUitls Post:url parameters:params success:^(id response) {
        
        NSDictionary *dict=response;
        IndexCarModel *carmode=[IndexCarModel indexcarWithDcit:dict];
        if ([carmode.state integerValue]==400) {
            [self.indexcararray removeAllObjects];
            NSMutableArray *marray=[NSMutableArray array];
            for (CarDetailmodel *cardetail in carmode.data) {
                CarlistCellMode *carlistcell=[[CarlistCellMode alloc]init];
                carlistcell.cardmode=cardetail;
                carlistcell.title=cardetail.shop_name;
                [marray addObject:carlistcell];
            }
            if (marray.count>=[Kpagesize integerValue]) {
                [self.tableView.footer resetNoMoreData];
            }else{
                [self.tableView.footer noticeNoMoreData];
            }
            [self.indexcararray addObjectsFromArray:marray];
            [self.tableView.header endRefreshing];
            [self.tableView reloadData];
            
        }else{
            if (self.conn) {
                [self.indexcararray removeAllObjects];
                [self.tableView reloadData];
            }
            [self.tableView.header endRefreshing];
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=carmode.msg;
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
            
            NSLog(@"%@",carmode.msg);
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
        [self.tableView.header endRefreshing];
    }];
    
    
}
-(NSString *)getswicthparmas:(NSDictionary *)tiaojian{
    
    NSString *cat_source_type=tiaojian[@"car_source_type"];
    CarbrandModel *brand=tiaojian[@"brand"];
    CarprogChildmode *child=tiaojian[@"cartory"];
    CarDetail *detail=tiaojian[@"cardetail"];
    NSString *kuwei=tiaojian[@"stock_postion"];
    NSString *caroutcolor=tiaojian[@"car_outside_color"];
    NSString *car_inside_color=tiaojian[@"car_inside_color"];
    NSString *sale_type=tiaojian[@"sale_type"];
    NSString *timer=tiaojian[@"timer"];
    NSMutableDictionary *conndict=[NSMutableDictionary dictionary];
    if (brand) {
        conndict[@"brand_name"]=brand.brand_name;
    }
    if (child) {
        conndict[@"cat_name"]=child.cat_name;
    }
    if (cat_source_type) {
        if([cat_source_type isEqualToString:@"全部平行进口车"]){
            conndict[@"expc"]=@"不限";
        }else if(![cat_source_type isEqualToString:@"中规"]){
            conndict[SourceType]=cat_source_type;
        }else{
            conndict[SourceType]=@"中规";
            if (detail) {
                conndict[@"mid"]=detail.ID;
            }
        }
        
    }
    if (kuwei) {
        conndict[@"stock_position"]=kuwei;
    }
    if (caroutcolor) {
        conndict[@"car_outside_color"]=caroutcolor;
    }
    if (car_inside_color) {
        conndict[@"car_inside_color"]=car_inside_color;
    }
    if (sale_type) {
        CatType *type=[CatType getCatTypeBytext:sale_type];
        
        conndict[@"sale_type"]=@([type.cat_id integerValue]);
        
    }
    if (timer) {
        if ([timer isEqualToString:@"期货"]) {
            conndict[@"timer"]=@"0";
        }else{
            conndict[@"timer"]=@"1";
            
        }
    }
    if (conndict.count>0) {
        return [NSString DataTOjsonString:conndict];
    }else{
        return @"";
    }
    
}

-(void)loadmoredata{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    self.isfromsearch= [XHUserDeflaut boolForKey:ISFROMSEARCH];
    self.searchcond=[XHUserDeflaut stringForKey:SEARCHCOND];
    self.searchconddetail=[XHUserDeflaut stringForKey:SEARCHCONDDetail];
    NSString *url;
    if (self.isfromsearch) {
        params[@"action"]=@"s_car";
        params[@"s_action"]=@"mydata";
        params[@"cond"]=self.searchcond;
        if (self.searchconddetail) {
            params[@"sesion"]=self.searchconddetail;
        }
        url=searchurl;
    }else{
        url=sscarurl;
        if (self.conn) {
            params[@"action"]=@"where";
            params[@"cond"]=self.conn;
            
        }else{
            params[@"action"]=@"where";
        }
    }
    params[@"num"]=Kpagesize;
    NSMutableArray *IDSarray=[NSMutableArray array];
    for (CarlistCellMode *mode in self.indexcararray) {
        [IDSarray addObject:mode.cardmode.ID];
    }
    params[@"page_id"]=[NSString DataTOjsonString:IDSarray];
    [PostUitls Post:url parameters:params success:^(id response) {
        NSDictionary *dict=response;
        IndexCarModel *carmode=[IndexCarModel indexcarWithDcit:dict];
        if ([carmode.state integerValue]==400) {
            NSMutableArray *marray=[NSMutableArray array];
            for (CarDetailmodel *cardetail in carmode.data) {
                CarlistCellMode *carlistcell=[[CarlistCellMode alloc]init];
                carlistcell.cardmode=cardetail;
                
                [marray addObject:carlistcell];
            }
            [self.indexcararray addObjectsFromArray:marray];
            
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
            
        }else{
            [self.tableView.footer noticeNoMoreData];
            if (self.conn) {
               
                [self.tableView reloadData];
            }
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=carmode.msg;
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
            NSLog(@"%@",carmode.msg);
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [self.tableView.footer endRefreshing];
    }];
}
-(void)setupRightItem:(BOOL)ishide{
    if (ishide) {
        self.navigationItem.rightBarButtonItem=nil;
    }else{
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"筛选" style:UIBarButtonItemStyleDone target:self action:@selector(sanxuanclick)];}
}

//-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
//    if ([searchBar.text isEqualToString:@""]) {
//        [XHUserDeflaut setBool:NO forKey:ISFROMSEARCH];
//       
//        [XHUserDeflaut setObject:nil forKey:SEARCHCOND];
//       
//       
//        [XHUserDeflaut setObject:nil forKey:SEARCHCONDDetail];
//        [self needupdate];
//
//    }
//}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    UITextField *searchBarTextField = nil;
    NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) ? searchBar.subviews : [[searchBar.subviews objectAtIndex:0] subviews];
    for (UIView *subview in views)
    {
        if ([subview isKindOfClass:[UITextField class]])
        {
            searchBarTextField = (UITextField *)subview;
            break;
        }
    }
    searchBarTextField.enablesReturnKeyAutomatically = NO;
}

//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];

//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}
//     ];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg"] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setTranslucent:YES];
//    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
//    if ([XHUserDeflaut boolForKey:SHOWFIND]) {
//        
//        self.findvc.view.hidden=YES;
//        NSLog(@"%@",self.view);
//        self.tableView.hidden=NO;
//        self.navigationItem.title=@"找车源";
//        [self setupRightItem:NO];
//        self.loading.hidden=YES;
//        self.isfromsearch= [XHUserDeflaut boolForKey:ISFROMSEARCH];
//        self.searchcond=[XHUserDeflaut stringForKey:SEARCHCOND];
//        self.searchconddetail=[XHUserDeflaut stringForKey:SEARCHCONDDetail];
//        self.isfromsearch= [XHUserDeflaut boolForKey:ISFROMSEARCH];
//        self.searchcond=[XHUserDeflaut stringForKey:SEARCHCOND];
//        if (self.isfromsearch&&self.searchcond !=nil) {
//            self.clearbtn.hidden=NO;
//            
//            
//        }else{
//            self.clearbtn.hidden=YES;
//        }
//     self.search.text=self.searchcond;
//        
//        
//    }else{
//        
//        self.findvc.view.hidden=NO;
//        NSLog(@"%@",self.view);
//        self.tableView.hidden=YES;
//        self.navigationItem.title=@"车源";
//        [self setupRightItem:YES];
//        self.loading.hidden=NO;
//        [self.findvc needupdatehistory];
//        
//    }
//}

-(void)clearConn{
    self.conn=nil;
    self.tiaojian=nil;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.search endEditing:YES];
}
-(void)sanxuanclick{
    SaixuanViewController *vc= [XHMianStory instantiateViewControllerWithIdentifier:@"saixuan"];
    vc.tiaojian=self.tiaojian;
    vc.delegate=self;
    self.isfromsearch=NO;
    self.searchcond=nil;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.search endEditing:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.tableView!=tableView) {
        return 44;
    }else{
    CarlistCellMode *cell=self.indexcararray[indexPath.row];
    return cell.rowheight;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (tableView !=self.tableView) {
        return self.searcharray.count;
    }else{
    return self.indexcararray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView !=self.tableView) {
        SearchMode *mode=self.searcharray[indexPath.row];
        SearchTableViewCell *cell=[SearchTableViewCell cellWithTablView:tableView];
        if (mode.car_name) {
            cell.name.text=mode.car_name;
        }else if(mode.title){
            cell.name.text=mode.title;
        }else if(mode.user_truename){
            cell.name.text=mode.user_truename;
        }else{
            cell.name.text=mode.com_name;
        }
        cell.count.text=mode.count;
        return cell;

    }else{
    CarTableCellTableViewCell *cell=[CarTableCellTableViewCell cellWithTableView:tableView];
    CarlistCellMode *cellmode=self.indexcararray[indexPath.row];
    cell.carcellmode=cellmode;
    return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
//    if (self.tableView!=tableView) {
//        [XHUserDeflaut setBool:YES forKey:ISFROMSEARCH];
//        SearchMode *mode=self.searcharray[indexPath.row];
//        [XHUserDeflaut setObject:self.searchDisplayController.searchBar.text forKey:SEARCHCOND];
//        NSMutableDictionary *params=[NSMutableDictionary dictionary];
//        params[@"car_name"]=mode.car_name;
//        [XHUserDeflaut setObject:[NSString DataTOjsonString:params] forKey:SEARCHCONDDetail];
//        
//        [self needupdate];
//    }else{
    CarDetailViewController *detailvc= [CarDetailViewController viewcontroller];
    CarlistCellMode *cellmode=self.indexcararray[indexPath.row];
    detailvc.ID=cellmode.cardmode.ID;
    [self.navigationController pushViewController:detailvc animated:YES];
//    }
}
- (void)setExtraCellLineHidden: (UITableView *)tableView{
    
    UIView *view =[ [UIView alloc]init];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
    
    [tableView setTableHeaderView:view];
    
}
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
